package com.leo.chapter15;

import java.util.concurrent.atomic.AtomicReference;

/**
 *
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class CasNumberRange {
    private static class IntPair{
        final int lower;
        final int upper;

        private IntPair(int lower, int upper) {
            this.lower = lower;
            this.upper = upper;
        }
    }

    private final AtomicReference<IntPair> values = new AtomicReference<>(new IntPair(0, 0));

    public int getLower(){ return values.get().lower; }
    public int getUpper(){ return values.get().upper; }
    public void setLower(int i){
        while (true){
            IntPair old = values.get();
            if(i > old.upper){
                throw new IllegalStateException("");
            }
            IntPair newV = new IntPair(i, old.upper);
            if (values.compareAndSet(old, newV)){
                return;
            }
        }
    }

    //....其余方法类似
}
